import { ref } from 'vue'
import { adapter, request } from '@/request'
import type { RequestWrapper } from '@/core/interfaces/common'
import { orderByPost } from '@/core/api/order'
import { message } from 'ant-design-vue'
import is from '@/utils/is'
import type { Industry } from '@/core/interfaces/industry'
import type { PlatformEnum, ProductEnum } from '@/core/enums/platform'
import type { CreativePictureEnum, TypeEnum } from '@/core/enums/type'
import type { AuthPack } from '@/core/interfaces/base'
import type { PriceSheet } from '@/core/interfaces/priceSheet'
import type { VersionTypeEnum } from '@/core/enums/type'
import type { ComboPricePack } from '@/core/interfaces/combo'
import type { PriceKeyword } from '@/core/interfaces/keyword'

export interface OrderCreator extends AuthPack, Omit<ComboPricePack, 'comboId'> {
  productId: ProductEnum
  productName: string
  priceAskId: PriceSheet['id']
  type: TypeEnum
  onLinePeriod: string
  customerProxyId: number
  assessmentPrice: number
  // 订单创建用的查价关键词的模型
  orderWords: PriceKeyword[]
  soldAreas: string
  versionType?: VersionTypeEnum
  industryCode: Industry['code']
  clientType: PlatformEnum
  marketPrice?: number
  hasCreatePic: CreativePictureEnum
  oldOrderId?: number
}

export default function submitOrder() {
  const service = adapter<RequestWrapper<any>>(request)
  const loading = ref(false)
  const response = ref<any>()

  const fetch = (data: OrderCreator) => {
    const { url, method } = orderByPost()
    loading.value = true

    return service({
      config: {
        url,
        method,
        data
      },
      error: (err) => {
        if (is.error(err)) {
          message.error(err.message)
        }
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
